package com.wangtianxiang.sxp.dbbase.config.db;

import org.springframework.util.Assert;

import static java.util.concurrent.TimeUnit.MINUTES;
import static java.util.concurrent.TimeUnit.SECONDS;

/**
 * 数据源属性
 * 
 * @author lgyu4
 *
 */
public class SxpDatasourceProperties {

	// HikariConfig can't init while exist these properties
	public static final  String[] PROPERTIES_IGNORE_FOR_HIKATI = { "typeAliasesPackage", "locationPatterns", "basePackage","dataSourceAlias" };

	private static final long CONNECTION_TIMEOUT = SECONDS.toMillis(30);
	private static final long VALIDATION_TIMEOUT = SECONDS.toMillis(5);
	private static final long IDLE_TIMEOUT = MINUTES.toMillis(10);
	private static final long MAX_LIFETIME = MINUTES.toMillis(30);

	// Properties changeable at runtime through the HikariConfigMXBean
	//
	private volatile String catalog;
	private volatile long connectionTimeout;
	private volatile long validationTimeout;
	private volatile long idleTimeout;
	private volatile long leakDetectionThreshold;
	private volatile long maxLifetime;
	private volatile int maximumPoolSize;
	private volatile int minimumIdle;
	private volatile String username;
	private volatile String password;

	// Properties NOT changeable at runtime
	//
	private long initializationFailTimeout;
	private String connectionInitSql;
	private String connectionTestQuery;
	private String dataSourceClassName;
	private String dataSourceJNDI;
	private String driverClassName;
	private String jdbcUrl;
	private String poolName;
	private String schema;
	private String transactionIsolationName;
	private boolean isAutoCommit = true;
	private boolean isReadOnly;
	private boolean isIsolateInternalQueries;
	private boolean isRegisterMbeans;
	private boolean isAllowPoolSuspension;

	/**
	 * package to scan for domain objects,use comma for multiple packages
	 */
	private String typeAliasesPackage;

	/**
	 * xml classpath for mysql,use comma for multiple packages
	 */
	private String locationPatterns;

	/**
	 * the base package for your mapper interface files.
	 */
	private String basePackage;

	/**
	 * alias for datasource,use comma for multiple alias
	 */
	private String dataSourceAlias;


	public SxpDatasourceProperties() {
		minimumIdle = -1;
		maximumPoolSize = -1;
		maxLifetime = MAX_LIFETIME;
		connectionTimeout = CONNECTION_TIMEOUT;
		validationTimeout = VALIDATION_TIMEOUT;
		idleTimeout = IDLE_TIMEOUT;
		initializationFailTimeout = 1;
	}

	public String getCatalog() {
		return catalog;
	}

	public void setCatalog(String catalog) {
		this.catalog = catalog;
	}

	public long getConnectionTimeout() {
		return connectionTimeout;
	}

	public void setConnectionTimeout(long connectionTimeout) {
		this.connectionTimeout = connectionTimeout;
	}

	public long getValidationTimeout() {
		return validationTimeout;
	}

	public void setValidationTimeout(long validationTimeout) {
		this.validationTimeout = validationTimeout;
	}

	public long getIdleTimeout() {
		return idleTimeout;
	}

	public void setIdleTimeout(long idleTimeout) {
		this.idleTimeout = idleTimeout;
	}

	public long getLeakDetectionThreshold() {
		return leakDetectionThreshold;
	}

	public void setLeakDetectionThreshold(long leakDetectionThreshold) {
		this.leakDetectionThreshold = leakDetectionThreshold;
	}

	public long getMaxLifetime() {
		return maxLifetime;
	}

	public void setMaxLifetime(long maxLifetime) {
		this.maxLifetime = maxLifetime;
	}

	public int getMaximumPoolSize() {
		return maximumPoolSize;
	}

	public void setMaximumPoolSize(int maximumPoolSize) {
		this.maximumPoolSize = maximumPoolSize;
	}

	public int getMinimumIdle() {
		return minimumIdle;
	}

	public void setMinimumIdle(int minimumIdle) {
		this.minimumIdle = minimumIdle;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public long getInitializationFailTimeout() {
		return initializationFailTimeout;
	}

	public void setInitializationFailTimeout(long initializationFailTimeout) {
		this.initializationFailTimeout = initializationFailTimeout;
	}

	public String getConnectionInitSql() {
		return connectionInitSql;
	}

	public void setConnectionInitSql(String connectionInitSql) {
		this.connectionInitSql = connectionInitSql;
	}

	public String getConnectionTestQuery() {
		return connectionTestQuery;
	}

	public void setConnectionTestQuery(String connectionTestQuery) {
		this.connectionTestQuery = connectionTestQuery;
	}

	public String getDataSourceClassName() {
		return dataSourceClassName;
	}

	public void setDataSourceClassName(String dataSourceClassName) {
		this.dataSourceClassName = dataSourceClassName;
	}

	public String getDataSourceJNDI() {
		return dataSourceJNDI;
	}

	public void setDataSourceJNDI(String dataSourceJNDI) {
		this.dataSourceJNDI = dataSourceJNDI;
	}

	public String getDriverClassName() {
		return driverClassName;
	}

	public void setDriverClassName(String driverClassName) {
		this.driverClassName = driverClassName;
	}

	public String getJdbcUrl() {
		return jdbcUrl;
	}

	public void setJdbcUrl(String jdbcUrl) {
		this.jdbcUrl = jdbcUrl;
	}

	public String getPoolName() {
		return poolName;
	}

	public void setPoolName(String poolName) {
		this.poolName = poolName;
	}

	public String getSchema() {
		return schema;
	}

	public void setSchema(String schema) {
		this.schema = schema;
	}

	public String getTransactionIsolationName() {
		return transactionIsolationName;
	}

	public void setTransactionIsolationName(String transactionIsolationName) {
		this.transactionIsolationName = transactionIsolationName;
	}

	public boolean isAutoCommit() {
		return isAutoCommit;
	}

	/**
	 * Default:	{@code true}
	 * @param isAutoCommit
	 */
	public void setAutoCommit(boolean isAutoCommit) {
		this.isAutoCommit = isAutoCommit;
	}

	public boolean isReadOnly() {
		return isReadOnly;
	}

	public void setReadOnly(boolean isReadOnly) {
		this.isReadOnly = isReadOnly;
	}

	public boolean isIsolateInternalQueries() {
		return isIsolateInternalQueries;
	}

	public void setIsolateInternalQueries(boolean isIsolateInternalQueries) {
		this.isIsolateInternalQueries = isIsolateInternalQueries;
	}

	public boolean isRegisterMbeans() {
		return isRegisterMbeans;
	}

	public void setRegisterMbeans(boolean isRegisterMbeans) {
		this.isRegisterMbeans = isRegisterMbeans;
	}

	public boolean isAllowPoolSuspension() {
		return isAllowPoolSuspension;
	}

	public void setAllowPoolSuspension(boolean isAllowPoolSuspension) {
		this.isAllowPoolSuspension = isAllowPoolSuspension;
	}

	public String getTypeAliasesPackage() {
		return typeAliasesPackage;
	}

	public void setTypeAliasesPackage(String typeAliasesPackage) {
		this.typeAliasesPackage = typeAliasesPackage;
	}

	public String getLocationPatterns() {
		return locationPatterns;
	}

	public void setLocationPatterns(String locationPatterns) {
		this.locationPatterns = locationPatterns;
	}

	public String getBasePackage() {
		return basePackage;
	}

	public void setBasePackage(String basePackage) {
		this.basePackage = basePackage;
	}

	public String getDataSourceAlias() {
		return dataSourceAlias;
	}

	public void setDataSourceAlias(String dataSourceAlias) {
		this.dataSourceAlias = dataSourceAlias;
	}

	public void afterPropertiesSet() throws Exception {
		Assert.hasText(typeAliasesPackage, "typeAliasesPackage must not be empty");
		Assert.hasText(locationPatterns, "locationPatterns must not be empty");
		Assert.hasText(basePackage, "basePackage must not be empty");
	}
}
